package com.example.utils;

import com.example.config.MyJwtProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MyJwtUtils {

    @Autowired
    private MyJwtProperties properties;

    public String createJwt(Map<String,Object> map){
        //生成令牌
        String s = Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.valueOf(properties.getSiginType()), properties.getKey())
                .setExpiration(new Date(System.currentTimeMillis() + properties.getTime()))
                .compact();
        return s;
    }
    //解析
    public Map<String,Object>  parseJwt(String jwt){
        Claims body = null;
        try {
            body = Jwts.parser()
                    .setSigningKey(properties.getKey())
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch  (Exception e) {
            // 仅仅只是打印异常信息而已
            e.printStackTrace();
        }
        return body;
    }
}
